package net.tngou.crawler.drug;

import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import javax.imageio.ImageIO;

import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;

import net.tngou.crawler.pojo.Drug;
import net.tngou.crawler.pojo.POJO;
import net.tngou.crawler.util.StringUtil;

public class Tesseract_OCR {
	private static final String CHARSET = "utf-8"; 
	private static final Logger log= LoggerFactory.getLogger(Tesseract_OCR.class);
	
	public  String GetCode(String url) throws IOException {
		
		if(!url.startsWith("http"))url="http:"+url;	
		log.info("访问图片{}",url);
		URL urlObj = new URL(url);		
		URLConnection connection = urlObj.openConnection();
		connection.connect();
		BufferedImage input = ImageIO.read(connection.getInputStream());
		if(input==null)return null; 
        BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(input);  
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));  
        Result result = null;  
        Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();  
        hints.put(DecodeHintType.CHARACTER_SET, CHARSET);  
        hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
//        hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
        try {
			result = new MultiFormatReader().decode(bitmap, hints);
			String resultStr = result.getText(); 
			return resultStr;
        } catch (NotFoundException e) {
		
			e.printStackTrace();
		}
		return null;  
        
	}
	
	
	public void updateBarcode(Drug drug) {
		System.err.println(drug.getId());
		JSONArray js = new JSONArray();
//		JSONArray array = JSON.parseArray(drug.getPicture());
//		if(StringUtil.isEmpty(drug.getPicture())||array==null)
//		{
//			drug.setBarcode(js.toJSONString());
//			drug.update();
//			return;
//		}
//		for (Object url : array) {
//			try {
//				String barcode= GetCode(url.toString());		
//				if(StringUtil.isNoneBlank(barcode)&&NumberUtils.isNumber(barcode)) {
//					js.add(barcode);		
//				}
//					
//			} catch (IOException e1) {
//		
//				e1.printStackTrace();
//			}
//		}
//		drug.setBarcode(js.toJSONString());
//		drug.update();
	}
	
	
	public static void main(String[] args) throws IOException {
		String url ="http://images.huatuoyf.com/product/2014/09/05/201409051520272.jpg";
		String r = new Tesseract_OCR().GetCode(url);
		System.err.println(r);
//		 /** 
//         * 设置输出文件的保存的文件目录 
//         */  
//		List<Drug> list = (List<Drug>) new Drug().list(1, 10);
//		list.forEach(e->{
//			JSONArray array = JSON.parseArray(e.getPicture());
//			array.forEach(url->{
//				
//				try {
//					String r = GetCode(url.toString());
//					if(StringUtil.isNoneBlank(r))
//						 System.err.println(r);
//				} catch (IOException e1) {
//					// TODO Auto-generated catch block
//					e1.printStackTrace();
//				}
//			
//			});
//		});
		
	        
	}
	
}
